package com.finance.logistics.anlyzier;


import com.finance.framework.lang.Privilege;
import com.finance.report.bean.pojo.LogisticsData;
import com.finance.system.bean.pojo.Region;
import com.finance.util.DateUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.client.utils.DateUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 收件地址解析类
 * Created by CBWL on 2017/4/27.
 */
@Component
public class ConsigneeAddressHandler extends AbstractHandler {

    @Override
    public void doHandler(LogisticsData logisticsData) {
        String provinceCode = logisticsData.getProvinceConsignee();
        String cityCode = logisticsData.getCityConsignee();
        String countyCode = logisticsData.getCountyConsignee();
        this.logger.info("省：{} 市：{} 区：{} 运单：{}", new String[]{provinceCode, cityCode, countyCode,logisticsData.getWaybillNum()});
         if(StringUtils.isBlank(provinceCode) && StringUtils.isBlank(cityCode) && StringUtils.isBlank(countyCode)){
             try {
                 this.grantAll(logisticsData);
                 this.getNextHandler().doHandler(logisticsData);
             } catch (Exception e) {
                 logger.error(e.getMessage(),e);
             }
         }else{
             Region region = this.regionCodeService.getByName(provinceCode,cityCode,countyCode);
             if(region == null){
                 try {
                     this.grantAll(logisticsData);
                     this.getNextHandler().doHandler(logisticsData);
                 } catch (Exception e) {
                     logger.error(e.getMessage(),e);
                 }
             }else{
                 String tableName = this.logisticsDataService.getDataTableName(logisticsData.getSignTime());
                 LogisticsData logisticsData2update = this.logisticsDataService.getByWaybillNum(tableName,logisticsData.getWaybillNum());
                 if(region.getLevel() == Region.LEVE_COUNTY){
                     logisticsData2update.setCountyConsignee(region.getCode());
                     logisticsData2update.setCityConsignee(region.getCity());
                     logisticsData2update.setProvinceConsignee(region.getProvince());
                 }else if(region.getLevel() == Region.LEVE_CITY){
                     logisticsData2update.setCityConsignee(region.getCity());
                     logisticsData2update.setProvinceConsignee(region.getProvince());
                 }else if(region.getLevel() == Region.LEVE_PROVINCE){
                     logisticsData2update.setProvinceConsignee(region.getCode());
                 }
                 try {
                     this.logisticsDataService.update(logisticsData2update);
                     this.grantAll(logisticsData);
                     this.logger.info("[解析时间计算]字段解析结束{} 运单号：{}", DateUtils.formatDate(new Date(), DateUtil.DEFAULT_FORMAT),logisticsData2update.getWaybillNum());
                     this.getNextHandler().doHandler(logisticsData);
                 } catch (Exception e) {
                     logger.error(e.getMessage(),e);
                 }
             }
         }
    }

    private void grantAll(LogisticsData logisticsData) throws Exception {
        List<Privilege> privilegeList = new ArrayList<Privilege>();
        CollectionUtils.addAll(privilegeList,new Privilege[]{Privilege.B10,Privilege.B11,Privilege.B12,Privilege.B13,Privilege.B14,Privilege.B15,Privilege.B16,Privilege.B17,Privilege.B18,Privilege.B19});
        this.updateDataVild(logisticsData,privilegeList);
    }
}
